﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.Xml.Serialization;


namespace senController.classes
{
    class XmlReader
    {
        XDocument xmlDoc;
        
        public XmlReader(string file)
        {
            xmlDoc = XDocument.Load(file);
        }

        /// <summary>
        /// Read Traffic from xml file.
        /// </summary>
        /// <returns>List with traffic objects</returns>
        public List<Traffic> readTraffic()
        {
            List<Traffic> traffic =
              (from vehicle in this.xmlDoc.Descendants("vehicle")
               select new Traffic()
               {
                   type = vehicle.Element("type").Value,
                   spawntime = vehicle.Element("spawnTime").Value,
                   location = vehicle.Element("location").Value,
                   direction = vehicle.Element("direction").Value,
               }).ToList<Traffic>();

            return traffic;
        }

        /// <summary>
        /// Read settings from xml file.
        /// </summary>
        /// <returns>Settings object</returns>
        public Settings readSettings()
        {
            List<Settings> settings =
              (from setting in this.xmlDoc.Descendants("settings")
               select new Settings()
               {
                   startDate = setting.Element("startDate").Value,
                   minGreenTime = setting.Element("minGreenTime").Value,
                   maxGreenTime = setting.Element("maxGreenTime").Value,
                   orangeTime = setting.Element("orangeTime").Value,
               }).ToList<Settings>();
            return settings[0];
        }
            
    }
}
